<template>
  <ContentWrap v-if="dept == 'manage' || dept == 'finance'">
    <el-row class="statistic">
      <el-col :span="8">
        <el-statistic title="收入" :value="statisticsData.income" :precision="2" />
      </el-col>
      <el-col :span="8">
        <el-statistic title="支出" :value="statisticsData.expenditure" :precision="2" />
      </el-col>
      <el-col :span="8">
        <el-statistic title="余额" :value="statisticsData.balance" :precision="2" />
      </el-col>
    </el-row>
  </ContentWrap>
  <ContentWrap v-if="dept == 'manage' || dept == 'finance'">
    <!-- 搜索工作栏 -->
    <el-form
      class="-mb-15px"
      :model="queryParams"
      ref="queryFormRef"
      :inline="true"
      label-width="100px"
    >
      <el-form-item label="分账时间" prop="divideTime">
        <el-date-picker
          v-model="queryParams.divideTime"
          class="!w-240px"
          end-placeholder="结束日期"
          start-placeholder="开始日期"
          type="daterange"
          value-format="YYYY-MM-DD HH:mm:ss"
        />
      </el-form-item>
      <el-form-item>
        <el-button @click="handleQuery"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetQuery"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
  </ContentWrap>

  <!-- 列表 -->
  <ContentWrap v-if="dept == 'manage' || dept == 'finance'">
    <el-table v-loading="loading" :data="list" :stripe="true" :show-overflow-tooltip="true">
      <el-table-column label="总分账编号" align="center" prop="code" />
      <el-table-column label="关联订单数量" align="center" prop="orderCount">
        <template #default="scope">
          <el-button link type="primary" @click="openOrderList(scope.row.code)">
            {{ scope.row.orderCount }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="关联人员数量" align="center" prop="userCount">
        <template #default="scope">
          <el-button link type="primary" @click="openEmployeeList(scope.row.code)">
            {{ scope.row.userCount }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="校验状态" align="center" prop="checkStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEIJIA_DIVIDE_CHECK_STATUS" :value="scope.row.checkStatus" />
        </template>
      </el-table-column>
      <el-table-column label="审核状态" align="center" prop="judgeStatus">
        <template #default="scope">
          <dict-tag :type="DICT_TYPE.MEIJIA_DIVIDE_JUDGE_STATUS" :value="scope.row.judgeStatus" />
        </template>
      </el-table-column>
      <el-table-column
        label="分账时间"
        align="center"
        prop="createTime"
        :formatter="dateFormatter"
        width="200px"
      >
        <template #default="scope">
          {{ formatDate(scope.row.divideTimeBegin, 'YYYY/MM/DD') }}-{{
            formatDate(scope.row.divideTimeEnd, 'YYYY/MM/DD')
          }}
        </template>
      </el-table-column>
      <el-table-column label="付款进度" align="center">
        <template #default="scope"> {{ scope.row.userCount }}-{{ scope.row.progress }} </template>
      </el-table-column>
      <el-table-column label="操作" align="center" min-width="110px">
        <template #default="scope">
          <el-button
            link
            type="primary"
            v-if="scope.row.judgeStatus != 2 && dept == 'finance'"
            @click="openJudgeStatus(scope.row.code, 2, '是否确认总账审核成功')"
          >
            审核成功
          </el-button>
          <el-button
            link
            type="danger"
            v-if="scope.row.judgeStatus == 0 && dept == 'finance'"
            @click="openJudgeStatus(scope.row.code, 2, '是否确认总账审核失败')"
          >
            审核失败
          </el-button>
          <el-button link type="danger" v-if="dept == 'finance'" @click="exportBill(scope.row.id)">
            下载财务表格
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页 -->
    <Pagination
      :total="total"
      v-model:page="queryParams.pageNo"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />
  </ContentWrap>
  <WalletOrderDrawer ref="walletOrderDrawer" />
  <WalletEmployeeDrawer ref="walletEmployeeDrawer" />
</template>

<script setup lang="ts">
import download from '@/utils/download'
import { dateFormatter, formatDate } from '@/utils/formatTime'
import WalletOrderDrawer from './WalletOrderDrawer.vue'
import WalletEmployeeDrawer from './WalletEmployeeDrawer.vue'
import * as WalletApi from '@/api/meijia/wallet'
import * as UserApi from '@/api/system/user'
import { DICT_TYPE, getIntDictOptions, getStrDictOptions } from '@/utils/dict'
defineOptions({ name: 'Wallet' })
/** 初始化 **/
onMounted(() => {
  showCurrentDept()
  getStatistics()
  getList()
})
const message = useMessage() // 消息弹窗
const { t } = useI18n() // 国际化

const loading = ref(true) // 列表的加载中
const list = ref([]) // 列表的数据
const total = ref(0) // 列表的总页数
const walletOrderDrawer = ref()
const walletEmployeeDrawer = ref()
const queryParams = reactive({
  pageNo: 1,
  pageSize: 10,
  name: undefined
})
const queryFormRef = ref() // 搜索的表单
const exportLoading = ref(false) // 导出的加载中
const statisticsData = ref({
  income: 0,
  expenditure: 0,
  balance: 0
})
const getStatistics = async () => {
  statisticsData.value = await WalletApi.getDivideAllStatistics()
}
/** 查询列表 */
const getList = async () => {
  loading.value = true
  try {
    const data = await WalletApi.getDivideAllPage(queryParams)
    list.value = data.list
    total.value = data.total
  } finally {
    loading.value = false
  }
}

/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.pageNo = 1
  getList()
}

/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value.resetFields()
  handleQuery()
}
const openOrderList = (code: string) => {
  walletOrderDrawer.value.open(code)
}
const openEmployeeList = (code: string) => {
  walletEmployeeDrawer.value.open(code)
}
const openJudgeStatus = async (code: string, status: number, content: string) => {
  await message.confirm(content, '审核')
  const data = { code: code, judgeStatus: status }
  await WalletApi.updateAllCodeStatus(data)
  message.success(t('common.success'))
  await getList()
}
const dept = ref('manage')
const show = ref(false)
const showCurrentDept = async () => {
  dept.value = await UserApi.getCurrentDept()
}
const exportBill = async (id: number) => {
  try {
    // 导出的二次确认
    await message.exportConfirm()
    // 发起导出
    exportLoading.value = true
    const data = await WalletApi.exportBillExcel({ id: id })
    download.excel(data, '微信财务账单.xls')
  } catch {
  } finally {
    exportLoading.value = false
  }
}
</script>
<style scoped lang="scss">
.statistic {
  text-align: center;
}
</style>
